﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using MergeTI.TiEntityObjects;
using System.Reflection;
using System.Collections;
using TiEntityObject.Notification;
using TiEntityObject.Contract;
using TiEntityObject.Protocol;
using MergeTI.TiEntityObjects.Clifar;

namespace TiPort.TiEntityObjects
{
    public class exportBase
    {
        public  virtual ICollection GetCollection()
        {
            throw new Exception("GetCollection не поддерживается");
        }

        protected virtual  string GetMergeProcedureName()
        {
            return string.Empty;
        }
    }

    public class export : exportBase
    {

        #region Notification
        [XmlElement("notificationZK")]
        public List<Notification> NotificationZK { get; set; }

        [XmlElement("notificationOK")]
        public List<Notification> NotificationOK { get; set; }

        [XmlElement("notificationEF")]
        public List<Notification> NotificationEF { get; set; }
        
        [XmlElement("notificationPO")]
        public List<Notification> NotificationPO { get; set; }

        [XmlElement("notificationSZ")]
        public List<Notification> NotificationSZ { get; set; }

        [XmlElement("notificationCancel")]
        public List<Notification> NotificationCancel { get; set; }
        #endregion

        #region Contract
        [XmlElement("contract")]
        public List<Contract> ContractList { get; set; }

        [XmlElement("contractProcedure")]
        public List<ContractProcedure> ContractProcedureList { get; set; }
        #endregion

        #region Protocol
        [XmlElement("protocolEF1")]
        public List<Protocol> ProtocolListEF1 { get; set; }

        [XmlElement("protocolOK1")]
        public List<Protocol> ProtocolListOK1 { get; set; }

        [XmlElement("protocolOK2")]
        public List<Protocol> ProtocolListOK2 { get; set; }     
        
        [XmlElement("protocolOK3")]
        public List<Protocol> ProtocolListOK3 { get; set; }

        [XmlElement("protocolEF2")]
        public List<Protocol> ProtocolListEF2 { get; set; }

        [XmlElement("protocolEF3")]
        public List<Protocol> ProtocolListEF3 { get; set; }

        [XmlElement("protocolPO1")]
        public List<Protocol> ProtocolListPO1 { get; set; }

        [XmlElement("protocolZK1")]
        public List<Protocol> ProtocolListZK1 { get; set; }

        [XmlElement("protocolZK5")]
        public List<Protocol> ProtocolListZK5 { get; set; }

        [XmlElement("protocolEvasion")]
        public List<Protocol> ProtocolEvasionList { get; set; }

        [XmlElement("protocolCancel")]
        public List<Protocol> ProtocolCancelType { get; set; }

        #endregion

        public override ICollection GetCollection()
        {
            Type myT = this.GetType();
            string propName = string.Empty;

            foreach (PropertyInfo p in myT.GetProperties())
            {
                object v = p.GetValue(this, null);
                if (((System.Collections.ICollection)v).Count > 0)
                {
                    return SetObjectTypeProperty((System.Collections.ICollection)v,p.Name);
                }
            }
            object[] r ={};
            return r;
        }

        private ICollection SetObjectTypeProperty(ICollection coll,string objectTypeName)
        {
            foreach (IDbRootObject i in coll)
            {
                i.ObjectType = objectTypeName;
            }
            return coll;
        }


    }

    public class masterDataBatch : exportBase
    {
        [XmlElement("organization")]
        public List<DictionaryOrganization> OrganizationList { get; set; }

        public override ICollection GetCollection()
        {
            return OrganizationList;
        }
    }

    public class dataroot : exportBase
    {
        [XmlElement("Inn")]
        public List<Inn> List { get; set; }

        public override ICollection GetCollection()
        {
            return List;
        }
    }
    
}
